package com.yxq.mdt.app.modules.video.adapter;

import android.content.Intent;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bigkoo.convenientbanner.ConvenientBanner;
import com.bigkoo.convenientbanner.holder.CBViewHolderCreator;
import com.bigkoo.convenientbanner.holder.Holder;
import com.bigkoo.convenientbanner.utils.ScreenUtil;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hyphenate.chat.EMClient;
import com.hyphenate.exceptions.EMServiceNotReadyException;
import com.orhanobut.hawk.Hawk;
import com.yxq.mdt.app.R;
import com.yxq.mdt.app.constant.Constant;
import com.yxq.mdt.app.constant.SPKey;
import com.yxq.mdt.app.dialog.CostumerDialog;
import com.yxq.mdt.app.glide.GlideHelper;
import com.yxq.mdt.app.modules.personal.bean.MyPersonalBean;
import com.yxq.mdt.app.modules.personal.ui.activity.RechargeActivity;
import com.yxq.mdt.app.modules.socialcontact.model.MainSocialContactModel;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.SocialPersonalHomepageAc;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.VideoInviteChatActivity;
import com.yxq.mdt.app.modules.socialcontact.ui.activity.VoiceInviteChatActivity;
import com.yxq.mdt.app.modules.video.bean.MainVideoBean;
import com.yxq.mdt.app.modules.video.bean.NearbyBannerBean;
import com.yxq.mdt.app.modules.video.bean.VideoNearbyBean;
import com.yxq.mdt.app.modules.video.bean.VideoNearbyListBean;
import com.yxq.mdt.app.modules.video.ui.activity.DisplayVideoAc;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.utils.GridSpacingItemDecoration;

import java.util.List;

import static com.blankj.utilcode.util.ActivityUtils.startActivity;

public class NearbyAdapter extends BaseMultiItemQuickAdapter<MainVideoBean, BaseViewHolder> {
    String localUserId;
    public CountDownTimer countDownTimer;
    public NearbyAdapter(List<MainVideoBean> data,String userId) {
        super(data);
        addItemType(MainVideoBean.MALL_ITEM_TYPE_BANNER, R.layout.nearby_layout_item_banner);
        addItemType(MainVideoBean.MALL_ITEM_TYPE_GRID_MENU, R.layout.layout_item_nearby_list);
        this.localUserId = userId;
    }

    @Override
    protected void convert(BaseViewHolder helper, MainVideoBean item) {
        switch (item.getItemType()) {
            case MainVideoBean.MALL_ITEM_TYPE_BANNER://顶部推荐直播间
                ConvenientBanner<VideoNearbyListBean> banner = helper.getView(R.id.banner);
                banner.setPageIndicator(new int[]{R.drawable.shape_banner_selector_unchecked, R.drawable.shape_banner_selector_checked});
                banner.setPages(new CBViewHolderCreator() {
                    @Override
                    public Holder createHolder(View itemView) {
                        return new ImageHolderView(itemView);
                    }

                    @Override
                    public int getLayoutId() {
                        return R.layout.layout_nearby_banner_item;
                    }
                }, ((NearbyBannerBean) item).getBannerListBean());
                if (!banner.isTurning()) {
                    banner.startTurning();
                }
                break;
            case MainVideoBean.MALL_ITEM_TYPE_GRID_MENU://下面直播间
                RecyclerView menuView = helper.getView(R.id.recyclerView);
                menuView.setLayoutManager(new GridLayoutManager(mContext, 2));
                VideoNearbyBean videoNearbyBean = ((VideoNearbyBean) item);
                NearbyDataListAdapter adapter = new NearbyDataListAdapter(R.layout.item_chat_list, videoNearbyBean.getNavIconListBeanList());
                menuView.setAdapter(adapter);
                if (menuView.getItemDecorationCount() == 0) {
                    menuView.addItemDecoration(new GridSpacingItemDecoration(2, ScreenUtil.dip2px(mContext, 15), true));
                }
                adapter.setEmptyView(R.layout.layout_empty_view, menuView);
                adapter.isUseEmpty(!(adapter.getData().size() > 0));
                adapter.setOnItemChildClickListener((adapter1, view, position) -> {
                    switch (view.getId()) {
                        case R.id.open_chat_lint_one:
                            Intent intent = new Intent(mContext, SocialPersonalHomepageAc.class);
                            intent.putExtra("userId", videoNearbyBean.getNavIconListBeanList().get(position).getUserId());
                            startActivity(intent);
                            break;
                        case R.id.item_number:
                            //语音播放
                            break;
                        case R.id.item_number_video:
                            //视频播放
                            if (!TextUtils.isEmpty(videoNearbyBean.getNavIconListBeanList().get(position).getVideoDetails())) {
                                Intent videoIntent = new Intent(mContext, DisplayVideoAc.class);
                                videoIntent.putExtra("userId", videoNearbyBean.getNavIconListBeanList().get(position).getUserId());
                                videoIntent.putExtra("video", videoNearbyBean.getNavIconListBeanList().get(position).getVideoDetails());
                                videoIntent.putExtra("age", videoNearbyBean.getNavIconListBeanList().get(position).getAge());
                                videoIntent.putExtra("headimg", videoNearbyBean.getNavIconListBeanList().get(position).getHeadimg());
                                videoIntent.putExtra("sex", videoNearbyBean.getNavIconListBeanList().get(position).getSex());
                                videoIntent.putExtra("residence", videoNearbyBean.getNavIconListBeanList().get(position).getAddress());
                                videoIntent.putExtra("name", videoNearbyBean.getNavIconListBeanList().get(position).getName());
                                videoIntent.putExtra("money", videoNearbyBean.getNavIconListBeanList().get(position).getMoney());
                                startActivity(videoIntent);
                            } else {
                                Toast.makeText(mContext, "该用户暂未上传视频", Toast.LENGTH_LONG).show();
                            }
                            break;
                        case R.id.is_phone_layout:
                            //视频或者音频聊天
                            String userName = "yxq" + videoNearbyBean.getNavIconListBeanList().get(position).getUserId();
                            if (userName.equals("yxq" + localUserId)) {
                                ToastUtils.showShort("不能和自己通话");
                                return;
                            }
                            if (Constant.getSocialUser().getMoney() < videoNearbyBean.getNavIconListBeanList().get(position).getPrice()) {

                                CostumerDialog dialog = new CostumerDialog.Builder(mContext, R.style.custom_dialog_no_match)
                                        .setContentView(R.layout.dialog_clear_cache_layout)
                                        .setCancelable(true)
                                        .create();

                                TextView title = dialog.findViewById(R.id.tv_title);

                                title.setText("余额不足");

                                TextView content = dialog.findViewById(R.id.tv_content);
                                content.setText("当前余额不足，充值后可继续送礼，是否去充值？");

                                //充值
                                TextView sure = dialog.findViewById(R.id.tv_sure);
                                sure.setText("充值");
                                sure.setOnClickListener(v -> {
                                    Intent it = new Intent(mContext, RechargeActivity.class);
                                    startActivity(it);
                                    dialog.dismiss();
                                });

                                //取消
                                TextView cancel = dialog.findViewById(R.id.tv_cancel);
                                cancel.setText("取消");
                                cancel.setOnClickListener(v -> {
                                    dialog.dismiss();
                                });
                                dialog.show();
                                return;
                            }
                            if (videoNearbyBean.getNavIconListBeanList().get(position).getAnInt() == 1) {
                                //语音
                                Intent goToChat = new Intent(mContext, VoiceInviteChatActivity.class);
                                goToChat.putExtra("userId", userName);
                                goToChat.putExtra("nickname", videoNearbyBean.getNavIconListBeanList().get(position).getName());
                                startActivity(goToChat);
                            } else {
                                //视频
                                //跳转视频聊天界面
                                Intent goToChat = new Intent(mContext, VideoInviteChatActivity.class);
                                goToChat.putExtra("userId", userName);
                                goToChat.putExtra("nickname", videoNearbyBean.getNavIconListBeanList().get(position).getName());
                                startActivity(goToChat);
                            }
                            break;
                        default:
                    }
                });
        }

    }

    /**
     * 图片轮转实现
     */
    class ImageHolderView extends Holder<VideoNearbyListBean> {
        private ImageView imageView;

        public ImageHolderView(View itemView) {
            super(itemView);
        }

        @Override
        protected void initView(View itemView) {
            imageView = itemView.findViewById(R.id.nearby_banner_image);
            imageView.setScaleType(ImageView.ScaleType.FIT_XY);
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
        }

        @Override
        public void updateUI(VideoNearbyListBean bean) {
            GlideHelper.setLoadRoundImage(mContext, bean.getPic_url(), R.color.gray, imageView);
        }
    }
}
